package org.wxl.liangpicturebackend.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.wxl.liangpicturebackend.common.BaseResponse;
import org.wxl.liangpicturebackend.common.ResultUtils;

//用于定义全局异常处理和控制器增强逻辑
@RestControllerAdvice
//自动生成日志记录器，简化日志输出。
@Slf4j
public class GlobalExceptionHandler  {
    //捕获异常，并处理异常
    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> handleException(BusinessException e){
        log.error("业务异常：",e);
        return ResultUtils.error(e.getCode(),e.getMessage());
    }
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> handleException(RuntimeException e){
        log.error("运行时异常：",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"系统错误");
    }
}
